package com.quhaodian.article.data.service.impl;

import com.quhaodian.article.data.dao.ArticleCommentDao;
import com.quhaodian.article.data.dao.ArticleLikeDao;
import com.quhaodian.article.data.entity.ArticleComment;
import com.quhaodian.article.data.entity.ArticleLike;
import com.quhaodian.article.data.request.ArticleLikeRequest;
import com.quhaodian.article.data.response.ArticleLikeResponse;
import com.quhaodian.data.core.Finder;
import com.quhaodian.discover.rest.base.ResponseObject;
import com.quhaodian.user.data.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.quhaodian.data.core.Updater;
import com.quhaodian.article.data.dao.ArticleDao;
import com.quhaodian.article.data.entity.Article;
import com.quhaodian.article.data.service.ArticleService;

import com.quhaodian.data.page.Filter;
import com.quhaodian.data.page.Order;
import com.quhaodian.data.page.Page;
import com.quhaodian.data.page.Pageable;

import java.util.List;

import com.quhaodian.data.utils.FilterUtils;
import org.springframework.context.annotation.Scope;


/**
 * Created by imake on 2017年08月15日09:52:12.
 */


@Scope("prototype")
@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {

    private ArticleDao dao;


    @Autowired
    private ArticleLikeDao articleLikeDao;

    @Autowired
    private ArticleCommentDao articleCommentDao;

    @Override
    @Transactional(readOnly = true)
    public Article findById(Long id) {
        return dao.findById(id);
    }


    @Override
    @Transactional
    public Article save(Article bean) {
        dao.save(bean);
        return bean;
    }

    @Override
    @Transactional
    public Article update(Article bean) {
        Updater<Article> updater = new Updater<Article>(bean);
        return dao.updateByUpdater(updater);
    }

    @Override
    @Transactional
    public Article deleteById(Long id) {
        Article bean = dao.findById(id);
        dao.deleteById(id);
        return bean;
    }

    @Override
    @Transactional
    public Article[] deleteByIds(Long[] ids) {
        Article[] beans = new Article[ids.length];
        for (int i = 0, len = ids.length; i < len; i++) {
            beans[i] = deleteById(ids[i]);
        }
        return beans;
    }

    @Override
    public ArticleLikeResponse like(ArticleLikeRequest request) {
        ArticleLikeResponse result = new ArticleLikeResponse();

        Article article = dao.findById(request.getArticle());
        if (article == null) {
            result.setCode(-1);
            result.setMsg("该文章不存在");
            return result;
        }
        Finder finder = Finder.create();
        finder.append("from ArticleLike a where a.article.id=:article");
        finder.append(" and a.user.id=:user");
        finder.setParam("article", request.getArticle());
        finder.setParam("user", request.getUser());

        ArticleLike like = articleLikeDao.findOne(finder);
        if (like == null) {
            Integer likes = article.getLikes();
            if (likes == null) {
                likes = 0;
            }
            likes++;
            article.setLikes(likes);
            ArticleLike bean = new ArticleLike();
            bean.setUser(UserInfo.fromId(request.getUser()));
            bean.setArticle(article);
            articleLikeDao.save(bean);
        } else {
            result.setCode(-2);
            result.setMsg("你已经喜欢过了");
        }
        return result;
    }

    @Override
    public ResponseObject comment(ArticleComment comment) {
        ResponseObject result = new ResponseObject();
        if (comment.getArticle()==null){
            result.setCode(-1);
            result.setMsg("文章不存在");
            return result;
        }
        Article article = dao.findById(comment.getArticle().getId());
        if (article == null) {
            result.setCode(-1);
            result.setMsg("该文章不存在");
            return result;
        }

        Integer comments = article.getComments();
        if (comments == null) {
            comments = 0;
        }
        comments++;
        article.setComments(comments);

        articleCommentDao.save(comment);


        return result;
    }


    @Autowired
    public void setDao(ArticleDao dao) {
        this.dao = dao;
    }

    @Override
    public Page<Article> page(Pageable pageable) {
        return dao.page(pageable);
    }


    @Override
    public Page<Article> page(Pageable pageable, Object search) {
        List<Filter> filters = FilterUtils.getFilters(search);
        if (filters != null) {
            pageable.getFilters().addAll(filters);
        }
        return dao.page(pageable);
    }

    @Override
    public List<Article> list(int first, Integer size, List<Filter> filters, List<Order> orders) {
        return dao.list(first, size, filters, orders);
    }
}